home *** CD-ROM | disk | FTP | other *** search
- unit HVHeaps;
- //
- // Simple wrapper classes around the Win32 Heap functions.
- // Written by Hallvard Vassbotn (hallvard@falcon.no), January 1999
- //
- interface
-
- uses
- Windows;
-
- type
- // The TPrivateHeap class gives basic memory allocation capability
- // The benefit of using this class instead of the native GetMem
- // and FreeMem routines, is that the memory pages used will
- // be seperate from other allocations. This gives reduced
- // fragmentation.
- TPrivateHeap = class(TObject)
- private
- FHandle: THandle;
- FAllocationFlags: DWORD;
- function GetHandle: THandle;
- public
- destructor Destroy; override;
- procedure GetMem(var P{: pointer}; Size: DWORD); virtual;
- procedure FreeMem(P: pointer);
- function SizeOfMem(P: pointer): DWORD;
- property Handle: THandle read GetHandle;
- property AllocationFlags: DWORD read FAllocationFlags write FAllocationFlags;
- end;
-
- // The Code Heap adds the feature of allocating readable/writable
- // and executable memory blocks. This allows us to have safe
- // run-time generated code while not wasting as much memory
- // as calls to VirtualAlloc would have caused, while avoiding
- // the pitfalls of changing the protection flags of blocks
- // allocated with GetMem.
- TCodeHeap = class(TPrivateHeap)
- public
- procedure GetMem(var P{: pointer}; Size: DWORD); override;
- end;
-
- implementation
-
- uses
- SysUtils,
- D2Support;
-
- function Win32Handle(Handle: THandle): THandle;
- begin
- if Handle = 0 then
- RaiseLastWin32Error;
- Result := Handle;
- end;
-
- function Win32Pointer(P: Pointer): Pointer;
- begin
- if P = nil then
- RaiseLastWin32Error;
- Result := P;
- end;
-
- { TPrivateHeap }
-
- destructor TPrivateHeap.Destroy;
- begin
- if FHandle <> 0 then
- begin
- Win32Check(Windows.HeapDestroy(FHandle));
- FHandle := 0;
- end;
- inherited Destroy;
- end;
-
- procedure TPrivateHeap.FreeMem(P: pointer);
- begin
- Win32Check(Windows.HeapFree(Handle, 0, P));
- end;
-
- function TPrivateHeap.GetHandle: THandle;
- begin
- if FHandle = 0 then
- FHandle := Win32Handle(Windows.HeapCreate(0, 0, 0));
- Result := FHandle;
- end;
-
- procedure TPrivateHeap.GetMem(var P{: pointer}; Size: DWORD);
- begin
- Pointer(P) := Win32Pointer(Windows.HeapAlloc(Handle, AllocationFlags, Size));
- end;
-
- function TPrivateHeap.SizeOfMem(P: pointer): DWORD;
- begin
- Result := Windows.HeapSize(Handle, 0, P);
- // HeapSize does not set GetLastError, but returns $FFFFFFFF if it fails
- if Result = $FFFFFFFF then
- Result := 0;
- end;
-
- { TCodeHeap }
-
- procedure TCodeHeap.GetMem(var P{: pointer}; Size: DWORD);
- var
- Dummy: DWORD;
- begin
- inherited GetMem(P, Size);
- Win32Check(Windows.VirtualProtect(Pointer(P), Size, PAGE_EXECUTE_READWRITE, @Dummy));
- end;
-
- end.
-
-
-
-